Xbasic

varToJSONHash Function

Syntax

C jsonHash = varToJSONHash(P propertyArray, C hashProperty [, L flagSpecialTags [, L flagCondense [, L flagUseDoubleQuotes ]]])

Arguments

propertyArrayPointer

Xbasic property array

hashPropertyCharacter

Name of the property in the Xbasic array to be used as the hash key

flagSpecialTagsLogical

Specifies if the property array uses the special {javascript} directive to indicate that the JSON property should be generated as Javascript and not as a string. Default if false.

flagCondenseLogical

Indicates if the generated JSON should be condensed to eliminate line breaks.

flagUseDoubleQuotesLogical

Indicates if strings and property names should be double quoted. By default property names are not quoted and strings are single quoted.

Description

The varToJSONHash() Function converts an Xbasic property array to a JSON hash.

varToJSONHash() is used with the following. Consider this Xbasic property array:

dim p[0] as p
i = p.append()
p[i].name = "John Smith"
p[i].address = "123 Main Street"
p[i].age = 23

i = p.append()
p[i].name = "Fredia Malt"
p[i].address = "456 Center Lane"
p[i].age = 33

We can convert this to a JSON hash, using the 'name' property as the hash index as follows:

dim jsonHash as c 
jsonHash = varToJSONHash(p,"name")

The resulting JSON string looks like this:

?jsonHash
= {
	'John Smith' : {address: '123 Main Street',age: 23},
	'Fredia Malt' : {address: '456 Center Lane',age: 33}

}

In this next example we use the flagSpecialTags property

DIM p[0] as p
i = p.append()
p[i].name = "John Smith"
p[i].address = "123 Main Street"
p[i].age = 23
p[i].sayHello = "{javascript}function() { alert('hello John') }"

i = p.append()
p[i].name = "Fredia Malt"
p[i].address = "456 Center Lane"
p[i].age = 33

jsonHash = varToJSONHash(p,"name", .t.)

The resulting JSON string is:

?jsonHash
= {
	'John Smith' : {address: '123 Main Street',age: 23,sayHello: function() { alert('hello John') }},
	'Fredia Malt' : {address: '456 Center Lane',age: 33}

}

See Also